/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sql;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import logic.Varpool;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import javax.swing.JOptionPane;

/**
 *
 * @author Freshman
 */
public class Dbconnection {

    private Varpool vap = null;     //Variable für die zukünftige Varpoolinstanz
    private boolean useFileSettings = false;
    //Verbindungsaufbau
    private String dbn = ""; //Eingegebener Datenbankname
    private String usr = ""; //Eingabebenutzer
    private String pwd = ""; //Eingabepasswort

    //Konstruktor
    public Dbconnection(Varpool vap) {
        this.vap = vap; //Übergibt dieser Instanz der Klasse Dbconnection die Varpool-Instanz
        tryLocalSettingsLoad();
    }

    private void tryLocalSettingsLoad() {
        File config = new File("hcc.properties");
        if (config.exists()) {
            BufferedInputStream stream = null;
            try {
                stream = new BufferedInputStream(new FileInputStream(config));
                Properties prop = new Properties();
            } catch (FileNotFoundException ex) {
                this.useFileSettings = false;
                connect();
            }
            try {
                vap.getProperties().load(stream);
                this.useFileSettings = true;
            } catch (IOException ex) {
                this.useFileSettings = false;
                connect();
            }
            try {
                stream.close();
            } catch (IOException ex) {
                System.exit(0);
            }
            vap.setDbname(vap.getProperties().getProperty("Datenbank"));
            vap.setLoginname(vap.getProperties().getProperty("Loginname"));
            vap.setPassword(vap.getProperties().getProperty("Passwort"));
        }
        connect();
    }

    private void connect() {
        if (useFileSettings == true) {
            connectAuto();
        } else {
            connectManual();
        }
    }

    private void connectAuto() {
        this.dbn = vap.getDbname();
        this.usr = vap.getLoginname();
        this.pwd = vap.getPassword();

        establish();
    }

    private void connectManual() {

        //Datenbank
        dbn = JOptionPane.showInputDialog("Bitte den Namen der zu öffnenden Datenbank eingeben!");
        if (dbn == null) {
            System.exit(0);
        }
        vap.setDbname(dbn);

        //Benutzername
        usr = JOptionPane.showInputDialog("Mit welchem Benutzer möchten sie sich anmelden?");
        if (usr == null) {
            System.exit(0);
        }
        vap.setLoginname(usr);
        
        //Passwort
        pwd = JOptionPane.showInputDialog("Benutzerpasswort eintragen (...falls vorhanden)");
        if (pwd == null) {
            System.exit(0);
        }
        vap.setPassword(pwd);
        establish();
    }

    private void establish() {
        try {
            //Connect
            Class.forName("com.mysql.jdbc.Driver");
            String s = "jdbc:mysql://localhost/";
            s = s + dbn;
            vap.setCon(DriverManager.getConnection(s, usr, pwd));
        } catch (SQLException ex) {
            if (useFileSettings == true) {
                JOptionPane.showMessageDialog(null, "Fehler: Konfigurationsdatei inkorrekt! Wechsle auf manuellen Modus", "SQL Exception", JOptionPane.ERROR_MESSAGE);
                this.useFileSettings = false;
            } else {
                JOptionPane.showMessageDialog(null, "Fehler beim öffnen der Datenbank: " + ex.getMessage(), "SQL Exception", JOptionPane.ERROR_MESSAGE);
            }
            connect();
        } catch (ClassNotFoundException ex) {
            JOptionPane.showMessageDialog(null, "Treiberklasse nicht gefunden: " + ex.getMessage(), "ClassNotFound Exception", JOptionPane.ERROR_MESSAGE);
            System.exit(0);
        }
    }
}
